package com.hxzy.shop.commodity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.hxzy.shop.commodity.convert.DrugConvert;
//import com.hxzy.shop.commodity.convert.PrescriptiondrugConvert;
import com.hxzy.shop.commodity.dto.req.*;
import com.hxzy.shop.commodity.dto.resp.*;
//import com.hxzy.shop.commodity.listener.PrescriptionImageListener;
//import com.hxzy.shop.commodity.listener.PrescriptiondrugListener;
import com.hxzy.shop.commodity.mapper.*;
//import com.hxzy.shop.commodity.producer.DrugProducer;
import com.hxzy.shop.commodity.service.*;
import com.hxzy.shop.entity.*;
import com.hxzy.shop.message.DrugChangeMessage;
import com.hxzy.shop.vo.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sifan
 * @PackagName:com.yimeng.shop.commodity.service.impl
 * @ClassName: PrescriptionServiceImpl
 * @Description:
 * @date:2021/8/9 9:26
 */
@Service
@Slf4j
public class PrescriptiondrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements PrescriptiondrugService {

    @Resource
    private DrugMapper drugMapper;

    @Resource
    private PrescriptiondrugMapper prescriptiondrugMapper;

    @Resource
    private ClassifiedManagementService classifiedManagementService;

    @Resource
    private  DrugCarouselMapService drugCarouselMapService;

    @Resource
    private DrugDetailsService drugDetailsService;

    @Resource
    private MarketingActivityTableMapper marketingActivityTableMapper;

    @Override
    public PageInfo<DrugResp> getDrugPage(DrugPageReq drugPageReq) {
        return PageHelper.startPage(drugPageReq.getPageNum(), drugPageReq.getPageSize())
                .doSelectPageInfo(() -> drugMapper.getPrescriptionPage(drugPageReq));
    }

    @Override
    public PageInfo<Prescriptiondrug> getPrescriptionList(DrugPageReq drugPageReq) {
        return PageHelper.startPage(drugPageReq.getPageNum(), drugPageReq.getPageSize())
                .doSelectPageInfo(() -> prescriptiondrugMapper.getPrescriptiondrugPage(drugPageReq));
    }

    @Override
    public DrugResp showPrescription(DrugInfoReq req) {
        // 查询药品基本参数
        DrugResp addDrugReq = drugMapper.getOneDrug(req);
        // 获得分类
        ClassResp classified = classifiedManagementService.getClassifiedBigAndSmall(req.getDrugId());
        //轮播图
        Map<Integer, String> drugCarouselMap = drugCarouselMapService.getCarouselMap(req.getDrugId());
        // 获取详细图
        Map<Integer, String> details = drugDetailsService.getDetails(req.getDrugId());
        if (!ObjectUtil.isEmpty(classified)) {
            addDrugReq.setStairClassifiedName(classified.getClassName());
            addDrugReq.setStairClassifiedId(classified.getClassId());
            if (!ObjectUtil.isEmpty(classified.getSonClassName())) {
                addDrugReq.setSecondLevelClassifiedName(classified.getSonClassName());
                addDrugReq.setSecondLevelClassifiedId(classified.getSonClassId());
            }
        }
        if (!ObjectUtil.isEmpty(drugCarouselMap)) {
            addDrugReq.setDrugCarouselMap(drugCarouselMap);
        }
        if (!ObjectUtil.isEmpty(details)) {
            addDrugReq.setDetailedImage(details);
        }
        return addDrugReq;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePrescription(DrugInfoReq drugInfoReq) {
        //若药品参加了活动 只有活动结束了才可以对药品的状态进行修改
        Long drugId = drugInfoReq.getDrugId();
        //药品参加了活动 并且活动未结束
        MarketingActivityTable marketing = marketingActivityTableMapper.selectOne(new LambdaQueryWrapper<MarketingActivityTable>()
                .eq(MarketingActivityTable::getDrugId, drugId)
                .eq(MarketingActivityTable::getActivitystatusEnd, true)
        );
        //正在参加活动
        if (ObjectUtil.isNotEmpty(marketing)) {
            throw new ApiException(500, "不能对参加活动的药品进行修改");
        }
        Drug drug = drugMapper.selectById(drugInfoReq.getDrugId());
        if (ObjectUtil.isEmpty(drug)) {
            return false;
        }
        if (drug.getState()) {
            throw new ApiException(500, drug.getDrugName() + "已上架不能删除");
        }
        drug.setDisable(true);
        if (drugMapper.updateById(drug) > 0) {
            //发送药品信息修改消息
            //drugProducer.sendDrugChangeMsg(new DrugChangeMessage().setDrugId(drug.getId()).setChangeType(3).setMerchantId(drug.getMerchantId()));
        }
        return true;
    }

}
